package com.mdnote.practice.dfs_bfs;

import java.awt.*;
import java.util.LinkedList;

/**
 * @author Rhythm-2019
 * @version 1.0
 * @date 2020/9/17
 * @description 岛屿数量
 */
public class LeetCode200 {

    public static void main(String[] args) {
        LeetCode200 leetCode200 = new LeetCode200();

        char[][] ints = {
                {'1', '1', '1'},
                {'0', '1', '0'},
                {'1', '1', '1'}

        };
        int count = leetCode200.numIslands(ints);
        System.out.println(count);
    }

    public int numIslands(char[][] grid) {
        // solute by DFS
        int count = soluteByDFS(grid);

//        int count = soluteByBFS(grid);
        return count;
    }

    private int soluteByBFS(char[][] grid) {
        int count = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if (grid[i][j] == '1') {
                    // 岛屿，进行
                    count++;
                    bfs(grid, i, j);
                }
            }
        }

        return count;
    }

    private void bfs(char[][] grid, int row, int column) {
        LinkedList<Point> queue = new LinkedList<>();
        queue.addLast(new Point(column, row));

        while (!queue.isEmpty()) {
            int size = queue.size();
            // x -> column y -> row  grid[row][column]
            for (int i = 0; i < size; i++) {
                Point point = queue.removeFirst();

                grid[point.y][point.x] = '0';

                if (point.x - 1 > 0 && grid[point.y][point.x - 1] == '1') {
                    queue.addLast(new Point(point.x - 1, point.y));
                }
                if (point.x + 1 < grid[0].length && grid[point.y][point.x + 1] == '1') {
                    queue.addLast(new Point(point.x + 1, point.y));
                }
                if (point.y - 1 > 0 && grid[point.y - 1][point.x] == '1') {
                    queue.addLast(new Point(point.x, point.y - 1));
                }
                if (point.y + 1 < grid.length && grid[point.y + 1][point.x] == '1') {
                    queue.addLast(new Point(point.x, point.y + 1));
                }

            }
        }
    }

    private int soluteByDFS(char[][] grid) {

        int count = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if (grid[i][j] == '1') {
                    // 岛屿，进行
                    count++;
                    dfs(grid, i, j);
                }
            }
        }

        return count;
    }

    private void dfs(char[][] grid, int row, int column) {
        if (grid[row][column] == '0') {
            return;
        }
        grid[row][column] = '0';
        // 上、下、左、右
        if (row - 1 >= 0) {
            dfs(grid, row - 1, column);
        }
        if (row + 1 < grid.length) {
            dfs(grid, row + 1, column);
        }
        if (column - 1 >= 0) {
            dfs(grid, row, column - 1);
        }
        if (column + 1 < grid[0].length) {
            dfs(grid, row, column + 1);
        }
    }
}
